package com.jiyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.chengyuanMapper;
import com.jiyun.mapper.juminMapper;
import com.jiyun.model.chengyuan.Chengyuan;
import com.jiyun.model.chengyuan.FamilyDto;
import com.jiyun.model.jumin.Jumin;
import com.jiyun.result.Result;
import com.jiyun.service.chengyuanService;
import com.jiyun.vo.chengyuan.ChengyuanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class chengyuanServiceImpl extends ServiceImpl<chengyuanMapper, Chengyuan> implements chengyuanService {
    @Autowired
    private chengyuanMapper chengyuanMapper;
    @Autowired
    private juminMapper juminMapper;


    @Override
    public List<ChengyuanVO> findGuanxi(Integer id) {
        List<ChengyuanVO> vos = new ArrayList<>();
        QueryWrapper<Chengyuan> w = new QueryWrapper<>();
        w.eq("oneid",id);
        List<Chengyuan> chengyuans = chengyuanMapper.selectList(w);
        if (chengyuans.size()>0){
            for (Chengyuan chengyuan : chengyuans) {
                ChengyuanVO vo = new ChengyuanVO();
                Jumin jumin = juminMapper.selectById(chengyuan.getTwoid());
                vo.setId(chengyuan.getTwoid());
                vo.setGuanxi(chengyuan.getGuanxi());
                vo.setName(jumin.getName());
                vo.setPhoto(jumin.getPhoto());
                vo.setSex(jumin.getSex());
                vos.add(vo);
            }
        }
        QueryWrapper<Chengyuan> w2 = new QueryWrapper<>();
        w2.eq("twoid",id);
        List<Chengyuan> chengyuans2 = chengyuanMapper.selectList(w2);
        if (chengyuans2.size()>0){
            for (Chengyuan chengyuan : chengyuans2) {
                ChengyuanVO vo = new ChengyuanVO();
                Jumin jumin = juminMapper.selectById(chengyuan.getOneid());
                vo.setId(chengyuan.getOneid());
                vo.setGuanxi(chengyuan.getGuanxi());
                vo.setName(jumin.getName());
                vo.setPhoto(jumin.getPhoto());
                vo.setSex(jumin.getSex());
                vos.add(vo);
            }
        }
        return vos;
    }

    @Override
    public void addFamilyMember(FamilyDto familyDto) {
        Chengyuan chengyuan = new Chengyuan();
        chengyuan.setOneid(familyDto.getJuminId());
        chengyuan.setTwoid(familyDto.getTwo());
        chengyuan.setGuanxi(familyDto.getFamilyRelationship());
        baseMapper.insert(chengyuan);
    }
}
